/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.service.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.BillContentsDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.BillsContentsTBL;
import lu.med.praxiswahl.wahlumed.model.beans.BillsTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.BillContentServiceInt;

/**
 * 
 * @author pkendzo
 */

@Stateless
@Remote({ BillContentServiceInt.class })
public class BillContentService implements BillContentServiceInt {

	private BillsContentsTBL tmpBCon;

	@EJB
	private BillContentsDAOInt billContentDao;

	public BillContentService() {
		tmpBCon = new BillsContentsTBL();
	}

	@Override
	public int createBillContents(BillsTBL bill, BillService actualService) {
		final int NUMBER_OF_BILLCONTENTS_BY_BILL = 20;
		// Set<BillsContentsTBL> contents = new HashSet<BillsContentsTBL>();

		for (int i = 0; i < NUMBER_OF_BILLCONTENTS_BY_BILL; i++) {
			BillsContentsTBL tmpBCon_1 = new BillsContentsTBL();

			// tmpBCon_1.setRechnungs_nummer(actualService.getBillByID(billNumber));
			// tmpBCon_1.setRechnungs_nummer(bill);
			tmpBCon_1.setCode_tarif("");
			tmpBCon_1.setDate_dent_heure("");
			tmpBCon_1.setH("");
			tmpBCon_1.setMed("");
			tmpBCon_1.setMed_pres("");
			tmpBCon_1.setMontant(0.0);
			tmpBCon_1.setNbr(0);

			// contents.add(tmpBCon_1);
			billContentDao.persist(tmpBCon_1);
			tmpBCon_1 = null;
		}

		// actualService.setContentsOfBill(contents);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public void getBillContentService(Long id) {

		tmpBCon = billContentDao.findBillsContentByID(id);
	}

	@Override
	public String getCODE_TARIF_LIBELLE_OBJECT() {
		return tmpBCon.getCode_tarif();
	}

	@Override
	public BillsContentsTBL getBillContentByID(Long id) {
		return billContentDao.findBillsContentByID(id);
	}

	@Override
	public String getDATE_DENT_HEURE() {
		return tmpBCon.getDate_dent_heure();
	}

	@Override
	public String getH() {
		return tmpBCon.getH();
	}

	@Override
	public String getMED() {
		return tmpBCon.getMed();
	}

	@Override
	public String getMED_PRES() {
		return tmpBCon.getMed_pres();
	}

	@Override
	public double getMONTANT() {
		return tmpBCon.getMontant();
	}

	@Override
	public void setCODE_TARIF_LIBELLE_OBJECT(String code_libelle) {
		tmpBCon.setCode_tarif(code_libelle);
		billContentDao.update(tmpBCon);
	}

	@Override
	public void setDATE_DENT_HEURE(String date_dent_heure) {
		tmpBCon.setDate_dent_heure(date_dent_heure);
		billContentDao.update(tmpBCon);
	}

	@Override
	public void setH(String h) {
		tmpBCon.setH(h);
		billContentDao.update(tmpBCon);
	}

	@Override
	public void setMED(String med) {
		tmpBCon.setMed(med);
		billContentDao.update(tmpBCon);
	}

	@Override
	public void setMED_PRES(String med_pres) {
		tmpBCon.setMed_pres(med_pres);
		billContentDao.update(tmpBCon);
	}

	@Override
	public void setMONTANT(double montant) {
		tmpBCon.setMontant(montant);
		billContentDao.update(tmpBCon);
	}

	/*
	 * @Override public long getBillNumber() { return
	 * tmpBCon.getRechnungs_nummer().getRechnung_nummer(); }
	 */

	@Override
	public Long[] getBillsContentByBillNumber(Long billNumber) {
		List<BillsContentsTBL> billContents = billContentDao
				.findBillsContentByBillNumber(billNumber);
		Long[] billContentIDs = new Long[billContents.size()];

		for (int i = 0; i < billContents.size(); i++) {
			billContentIDs[i] = billContents.get(i).getBillsContentsID();
		}
		return billContentIDs;
	}

	@Override
	public int getNBR() {
		return tmpBCon.getNbr();
	}

	@Override
	public void setNBR(int nbr) {
		tmpBCon.setNbr(nbr);

	}

	@Override
	public int saveBillContents(Long billNumber,
			BillsContentsTBL[] billContentsOfLine) {

		for (int i = 0; i < billContentsOfLine.length; i++) {

			billContentDao.persist(billContentsOfLine[i]);
		}

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public void updateBillContents(BillsContentsTBL item) {
		BillsContentsTBL itemClone = this.billContentDao
				.findBillsContentByID(item.getBillsContentsID());

		itemClone.setCode_tarif(item.getCode_tarif());
		itemClone.setDate_dent_heure(item.getDate_dent_heure());
		itemClone.setH(item.getH());
		itemClone.setMed(item.getMed());
		itemClone.setMed_pres(item.getMed_pres());
		itemClone.setMontant(item.getMontant());
		itemClone.setNbr(item.getNbr());
		itemClone.setRechnungs_nummer(item.getRechnungs_nummer());

		this.billContentDao.update(itemClone);

	}

	@Override
	public int removeBillContentItem(Long id) {
		billContentDao.delete(id);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public BillsContentsTBL[] getBillsContentItemsByBillNumber(Long billNumber) {
		Object[] billContents = billContentDao.findBillsContentByBillNumber(
				billNumber).toArray();

		BillsContentsTBL[] billContentsItem = new BillsContentsTBL[billContents.length];

		for (int i = 0; i < billContents.length; i++) {
			billContentsItem[i] = (BillsContentsTBL) billContents[i];
		}

		return billContentsItem;
	}

}
